/**
 *  This file is part of Google App Engine suppport in NetBeans IDE.
 *
 *  Google App Engine suppport in NetBeans IDE is free software: you can
 *  redistribute it and/or modify it under the terms of the GNU General
 *  Public License as published by the Free Software Foundation, either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  Google App Engine suppport in NetBeans IDE is distributed in the hope
 *  that it will be useful, but WITHOUT ANY WARRANTY; without even the
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Google App Engine suppport in NetBeans IDE.
 *  If not, see <http://www.gnu.org/licenses/>.
 */
package org.netbeans.modules.j2ee.appengine.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import org.netbeans.api.project.Project;
import org.netbeans.modules.j2ee.appengine.AppEngineDeploymentManager;
import org.netbeans.modules.j2ee.appengine.util.AppEnginePluginUtils;
import org.netbeans.spi.project.ui.LogicalViewProvider;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.ListView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.RequestProcessor;

/**
 * @author Michal Mocnak
 */
public class AppEngineProjectChooser extends javax.swing.JPanel implements PropertyChangeListener,
        ActionListener, Runnable, ExplorerManager.Provider {

    private ListView lv;
    private ExplorerManager explorer;
    private AppEngineDeploymentManager manager;

    /** Creates new form AppEngineProjectChooser */
    public AppEngineProjectChooser(AppEngineDeploymentManager manager) {
        initComponents();

        this.manager = manager;
        lv = new ListView();
        lv.setDefaultProcessor(this);
        lv.setPopupAllowed(false);
        lv.setTraversalAllowed(false);
        explorer = new ExplorerManager();
        explorer.addPropertyChangeListener(this);
        content.setLayout(new BorderLayout());
        content.add(lv, BorderLayout.CENTER);

        RequestProcessor.getDefault().post(this);
    }

    public ExplorerManager getExplorerManager() {
        return explorer;
    }

    public void propertyChange(PropertyChangeEvent evt) {
        Node[] selNodes = explorer.getSelectedNodes();

        if (selNodes.length == 1) {
            Project project = selNodes[0].getLookup().lookup(Project.class);

            // Set selected
            manager.setSelected(project);
        }
    }

    /** Loads dependencies outside EQ thread, updates tab state in EQ */
    public void run() {
        Project[] projects = AppEnginePluginUtils.getAppEngineProjects();

        final List<Node> toRet = new ArrayList<Node>();

        for (Project p : projects) {
            LogicalViewProvider lvp = p.getLookup().lookup(LogicalViewProvider.class);
            toRet.add(lvp.createLogicalView());
        }

        Children.Array ch = new Children.Array();
        ch.add(toRet.toArray(new Node[0]));
        Node root = new AbstractNode(ch);
        getExplorerManager().setRootContext(root);
    }

    public void actionPerformed(ActionEvent e) {
        // Nothing to do
        // Overriding default action
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scroll = new javax.swing.JScrollPane();
        content = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        content.setPreferredSize(new java.awt.Dimension(200, 100));

        org.jdesktop.layout.GroupLayout contentLayout = new org.jdesktop.layout.GroupLayout(content);
        content.setLayout(contentLayout);
        contentLayout.setHorizontalGroup(
            contentLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 261, Short.MAX_VALUE)
        );
        contentLayout.setVerticalGroup(
            contentLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        scroll.setViewportView(content);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(AppEngineProjectChooser.class, "AppEngineProjectChooser.jLabel1.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, scroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(scroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel content;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane scroll;
    // End of variables declaration//GEN-END:variables
}